package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lemuel on 2016/7/19.
 */
public final class ApiPpcUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiPpcUtil.class);

    private ApiPpcUtil() {

    }

    public static final String PPC_HOT_SYNC = Configuration.getString("ppc.hot.sync");

    public static boolean postPpcHotSync(long qipuId, long chatId, long startPlayTime,
                                         long stopPlayTime, int recommendRate, int hot,
                                         String programName, int isTest, int isDeleted) {
        Map<String, String> params = new HashMap<>();
        params.put("qipuId", qipuId + "");
        if (chatId != 0) {
            params.put("chatId", chatId + "");
        }
        params.put("startPlayTime", startPlayTime / 1000 * 1000 + "");
        params.put("stopPlayTime", stopPlayTime / 1000 * 1000 + "");
        params.put("recommend", recommendRate + "");
        params.put("hot", hot + "");
        params.put("programName", programName);
        params.put("isTest", isTest + "");
        params.put("deleteFlag", isDeleted + "");  // 0: not deleted    1: is deleted
        params.put("ts", System.currentTimeMillis() + "");

        for (int i = 0; i < 3; ++i) {
            JSONObject result = ApiHttpRequestUtil.doPost(PPC_HOT_SYNC, null, params);
            if (null != result && Constants.CODE_SUC.equals(result.getString("code"))) {
                logger.info("[ApiPpcUtil][ppchotsync][success]{}", JSON.toJSONString(params));
                return true;
            }
        }
        return false;
    }
}
